package com.site.blog.my.core.controller.admin;

import com.site.blog.my.core.service.TagService;
import com.site.blog.my.core.util.PageQueryUtil;
import com.site.blog.my.core.util.Result;
import com.site.blog.my.core.util.ResultGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName: my-blog
 * @description:
 * @author: zxf
 * @create: 2021-07-01 09:48
 **/
@Controller
@RequestMapping("/admin")
public class TagController {


    @Resource
    private TagService tagService;

    @GetMapping("/tags")
    public String tagPage(HttpServletRequest request){
        request.setAttribute("path","tags");
        return "admin/tag";
    }

    @GetMapping("/tags/list")
    @ResponseBody
    public Result list(@RequestParam Map<String,Object> params){
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))){
            return ResultGenerator.genFailResult("参数异常~");
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(tagService.getBlogTagPage(pageQueryUtil));
    }

    @PostMapping("/tags/save")
    @ResponseBody
    public Result save(@RequestParam(value = "tagName") String tagName){
        if (StringUtils.isEmpty(tagName)){
            return ResultGenerator.genFailResult("参数异常~");
        }
        if (tagService.saveTag(tagName)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult("标签名重复~");
        }
    }

    @PostMapping("/tags/delete")
    @ResponseBody
    public Result delete(@RequestBody Integer[] index){
        if(index == null || index.length < 1){
            return ResultGenerator.genFailResult("参数异常~");
        }
        if(tagService.deleteBatch(index)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult("有关联数据，请勿删除~");
        }
    }

}

